Ext.onReady(function() {
	
	//显示提示信息
	Ext.QuickTips.init();
	
	//统一设置提示风格，为side
	Ext.form.Field.prototype.msgTarget='side';	
	
	//片路径：一张1×1的透明图片，Ext使用它来和CSS一起创建内嵌的图标。 
	Ext.BLANK_IMAGE_URL = '../extjs/resources/images/default/s.gif';
	
	//checkbox选择模型
	var selModel = new Ext.grid.CheckboxSelectionModel();
	//数据源
 	var processDefinitionStore = new Ext.data.JsonStore({
			url: '../processDefinition/list.action',
			fields: ['deploymentId', 'id', 'name', 'version', 'imageResourceName'],
			root: 'data', 
			totalProperty: 'total' 
		})
	processDefinitionStore.load({
		params: {
			start: 0,
			limit: 10
		}
	});	
	
	//checkbox选择模型
	var selModel = new Ext.grid.CheckboxSelectionModel();
	
	//列模型
	var colModel = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer({header: '序号', width: 35}), //加入序号
		selModel, //加入checkbox选择模型
		{header: '标识', width: 100, dataIndex: 'id', sortable: true},
		{header: '名称', width: 100, dataIndex: 'name', sortable: true},
		{header: '版本', width: 100, dataIndex: 'version', sortable: true}
	])
	colModel.defaultWidth  = 100;
	
	var processDefinitionGrid = new Ext.grid.GridPanel({
		title: '流程定义列表',
		height: 450,
		autoWidth: true, 
		viewConfig: new Ext.grid.GridView({
			autoFill: true  
		}),
		stripeRows: true, 
		sm: selModel, 
		frame: true, 
		applyTo: 'processDefinition_div',
		store: processDefinitionStore,
		cm: colModel, //列模型
		tbar: new Ext.Toolbar([
			{text: '添加', icon: '../images/add.gif', handler: showAdd},
			{text: '删除', icon: '../images/delete.gif', handler: doDel},
			{text: '内容', icon: '../images/viewConent.gif', handler: viewContent},
			{text: '图片', icon: '../images/viewImage.gif', handler: viewImage}
		]),
		bbar: new Ext.PagingToolbar({
			store: processDefinitionStore, 
			pageSize: 10,
			displayInfo: true,
			plugins: new Ext.ux.ProgressBarPager()
		})
	})	
	
	var processDefinitionForm = new Ext.form.FormPanel({
		frame: true, 
		buttonAlign: 'center',
		fileUpload: true,
		items: [
			{
				xtype: 'textfield',
				inputType: 'file',
				name:'processDefinitionFile',
				fieldLabel: '流程定义文件（zip）',
				allowBlank: false
			}
		],
		buttons: [
			{text: '添加流程', icon: '../images/save.gif', handler: doSave},
			{text: '关闭窗口', icon: '../images/close.gif', handler: doClose}
		]	
	});		
	
	
	var win = new Ext.Window({
		width: 500,
		height: 200,
		items: [processDefinitionForm],
		modal: true, //模态
		resizable: false, //禁制调整窗口大小
		closeAction: 'hide' //不销毁form对象，只是隐藏
	})	
	
	function showAdd() {
		processDefinitionForm.getForm().reset();
		win.show();
	}
	
	function doSave() {
		processDefinitionForm.getForm().submit({
			url: '../processDefinition/add.action',
			method: 'POST',
			clientValidation: true,
			waitTitle: '提示',
			waitMsg: '正在部署流程，请稍后...',
			success: function() {
				processDefinitionStore.reload(); //重新加载数据
				Ext.Msg.alert('提示', '部署流程成功！', function() {
					win.hide();					
				});
			},
			failure: function() {
				Ext.Msg.alert('提示', '部署流程失败！');
			}
		})
	}
	
	function doClose() {
	
	}
	
	function doDel() {
		var records = processDefinitionGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择需要删除的流程！');
		}else {
			Ext.Msg.confirm('提示', '确认删除？', function(data) {
				if (data == 'yes') {
					var deploymentIds = [];	
					for (var i=0; i<records.length; i++) {
						deploymentIds.push(records[i].get('deploymentId'))
					}
					Ext.Msg.show({
						title: '提示',
						msg: '正在删除流程数据，请稍后...'
					})
					Ext.Ajax.request({
						url: '../processDefinition/del.action',
						method: 'GET',
						params: {deploymentIds:deploymentIds},
						callback: function(options, success, response ) {
							if (success) {
								processDefinitionStore.reload(); //重新装载
								Ext.Msg.alert('提示', '删除流程成功！');
							}else {
								Ext.Msg.alert('提示', '删除流程失败！');
							}
						}
					})
				}
			})
		}		
	}
	
	function viewContent() {
		var records = processDefinitionGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择流程！');
		}else if (records.length > 1) {
			Ext.Msg.alert('提示', '一次只能选择一个流程！');
		}else {
			openWin('../processDefinition/viewConent.action?deploymentIds=' + records[0].get('deploymentId'), 'viewConent', 600, 600, 1);
		} 		
	}
	
	function viewImage() {
		var records = processDefinitionGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择流程！');
		}else if (records.length > 1) {
			Ext.Msg.alert('提示', '一次只能选择一个流程！');
		}else {
			openWin('../processDefinition/viewImage.action?deploymentIds=' + records[0].get('deploymentId') + '&imageResourceName=' + records[0].get('imageResourceName'), 'viewImage', 600, 600, 1);
		} 
	}
})